package org.vectrics.recruiter.struts.admin;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionMessages;
import org.vectrics.company.Company;
import org.vectrics.employee.Employee;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Address;
import org.vectrics.location.Location;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * @author pgoette
 * Clase encargada de borrar las location que no se utilizan
 */
public class DeleteLocationAction extends VectricsStrutsAction{

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		
		String forward = "success";
		// recupero el id de location
		String idLocation = actionData.getRequest().getParameter("locationId");
		// si manda el id de la location
		if ((idLocation!=null)&&(!idLocation.trim().equals(""))) {
			// recupero la location
			Location location = RecruitServiceLocator.getLocationService()
			          .findLocation(new Long(idLocation));
			
			// Verifico que la location no tenga location que dependan de ella
			if ((location.getChildren() != null) && (!location.getChildren().isEmpty())) {
				actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location");        			
    			return "invalid";
			}
			
			// Verifico que no use la location ninguna compania 
			List<Company> companias = RecruitServiceLocator.getCompanyService().findAllCompany();
			for (Company company : companias) {
				if (company.getLocation()!=null) {
					if (company.getLocation().getId().equals(location.getId())) {
						actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location");        			
						return "invalid";
					}
				}
				if ((company.getAddress()!=null) && (company.getAddress().getLocation()!=null)) {
					if (company.getAddress().getLocation().getId().equals(location.getId())) {
						actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location");        			
						return "invalid";
					}
				}
			}
			
			// Verifico que no use la location ningun job Order
			List<Job> jobs = RecruitServiceLocator.getJobProvider().findJobs();
			for (Job job : jobs) {
				if (job.getLocation().getId().equals(location.getId())) {
					actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location");        			
	    			return "invalid";
				}
			}
			
			// Verifico que no use la location ninguna Direccion
			List<Address> addresses =  RecruitServiceLocator.getPersonService().findAllAddress();
			for(Address address : addresses) {
				if (address.getLocation().getId().equals(location.getId())) {
					actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location.in.use");        			
	    			return "invalid";					
				}
			}
			
			// Verifico que no use la location ningun empleado
			List<Employee> employees =  RecruitServiceLocator.getCompanyService().findAllEmployees();
			for(Employee employee : employees) {
				if (employee.getLocation().getId().equals(location.getId())) {
					actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location.in.use");        			
	    			return "invalid";					
				}
			}
			
			try {
				RecruitServiceLocator.getLocationService().deleteLocation(location);
				ThreadProperties.forcedCommit();
			} catch (Exception ex) {
				actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.location");        			
    			return "invalid";
			}
			
		}
		
		return forward;
	}

}
